package com.bx.implatform.portal.controller;

import com.bx.implatform.portal.entity.DelTodoEntity;
import com.bx.implatform.portal.entity.MessageEntity;
import com.bx.implatform.portal.entity.TodoEntity;
import com.bx.implatform.portal.entity.UpTodoEntity;
import com.bx.implatform.portal.service.PortalApiService;
import com.bx.implatform.result.Result;
import com.bx.implatform.result.ResultUtils;
import com.fasterxml.jackson.databind.JsonNode;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 门户控制器
 */
@RestController
@RequestMapping("/imApi/portal")
@Tag(name = "门户接口")
@RequiredArgsConstructor
public class PortalApiController {

    @Resource
    private PortalApiService portalApiService;


    @PostMapping("/v1/oauth2/getAccessToken")
    @Operation(summary = "获取accessToken", description = "根据终端类型获取token")
    public Result getAccessToken(@NotNull(message = "用户ID不能为空") @RequestParam String userId,@NotNull(message = "终端类型不能为空: 0:web 1:app 2:pc 3:大屏") @RequestParam Integer terminal) {
        return ResultUtils.success(portalApiService.getAccessToken(userId,terminal));
    }

    @GetMapping("/v1/dept/list")
    @Operation(summary = "获取部门列表", description = "根据当前用户的部门获取当前部门下的部门列表")
    public JsonNode deptList(@NotNull(message = "部门ID不能为空") @RequestParam String deptId) {
        return portalApiService.deptList(deptId);
    }

    @GetMapping("/v1/dept/detail")
    @Operation(summary = "获取部门详情", description = "获取当前部门的部门详情")
    public JsonNode deptDetail(@NotNull(message = "部门ID不能为空") @RequestParam String deptId) {
        return portalApiService.deptDetail(deptId);
    }

    @GetMapping("/v1/user/listByDeptId")
    @Operation(summary = "获取用户列表", description = "根据部门获取用户基本信息列表")
    public JsonNode userList(@NotNull(message = "部门ID不能为空") @RequestParam String deptId,
                             @NotNull(message = "页码不能为空") @RequestParam Long pageNum,
                             @NotNull(message = "size不能为空") @RequestParam Long pageSize) {
        return portalApiService.userList(deptId, pageNum, pageSize);
    }

    @GetMapping("/v1/user/detail")
    @Operation(summary = "获取用户详情", description = "根据用户ID获取用户详情")
    public JsonNode userDetail(@NotNull(message = "用户ID不能为空") @RequestParam String userId) {
        return portalApiService.userDetail(userId);
    }


    @PostMapping("/v1/todo/create")
    @Operation(summary = "创建待办/待阅", description = "创建待办/待阅")
    public JsonNode createTodo(@RequestBody TodoEntity todo) {
        return portalApiService.createTodo(todo);
    }

    @PostMapping("/v1/todo/update")
    @Operation(summary = "更新待办/待阅", description = "更新待办/待阅")
    public JsonNode updateTodo(@Valid @RequestBody UpTodoEntity todo) {
        return portalApiService.updateTodo(todo);
    }

    @PostMapping("/v1/todo/delete")
    @Operation(summary = "删除待办/待阅", description = "删除待办/待阅")
    public JsonNode deleteTodo(@Valid @RequestBody DelTodoEntity todo) {
        return portalApiService.deleteTodo(todo);
    }

    @PostMapping("/v1/message/send")
    @Operation(summary = "发送工作通知", description = "发送工作通知")
    public JsonNode sendMsg(@RequestBody MessageEntity msg) {
        return portalApiService.sendMsg(msg);
    }

}
